import pandas as pd
import os

repo_name_list = ["cmssw", "django", "kubernetes", "moby", "opencv", "pandas", "rails", "react", "rust",
                  "salt", "scikit-learn", "symfony", "tensorflow", "terraform", "yii2"]
all_alg_list = [
    "MART",
    "RankBoost",
    "AdaRank",
    "Coordinate",
    "LambdaMART",
    "ListNet",
    "RandomForest",
    "FIFO",
    "original",
    "SSF"
]
for repo_name in repo_name_list:
    for alg_name in all_alg_list:
        file_name = "./Process Simulation/Results/" + repo_name + "/Results/" + alg_name + ".xlsx"
        #判断文件是否存在，不存在则继续
        if not os.path.exists(file_name):
            print(file_name + " not exist")
            continue
        # 读取xlsx文件
        df = pd.read_excel(file_name)

        # 1. 修改表头
        df.columns = ['Strategy', 'EC1', 'EC2', 'ED1', 'ED2', 'Sum']

        # 2. 根据行数修改第一列数据
        if df.shape[0] == 10:
            strategies = ['Top-1 (Keywords)', 'Top-1 (LOC)', 'Rear-1', 'Top-1 OR Rear-1',
                          'Top-10% OR Rear-10%', 'Top-10%', 'Top-10% OR Rear-10%',
                          'Top-20% OR Rear-20%', 'Top-20%', 'Rear-20%']
        elif df.shape[0] == 2:
            strategies = ['Top-1 (Keywords)', 'Top-1 (LOC)']
        else:
            raise ValueError("Unexpected number of rows")

        df['Strategy'] = strategies
        if alg_name == "SMF":
            # 将处理后的数据写回原文件或者新的文件
            df.to_excel("./Process Simulation/Results/" + repo_name + "/Results/SSF.xlsx", index=False)
        else:
            # 将处理后的数据写回原文件或者新的文件
            df.to_excel(file_name, index=False)
